from app import Strategy
from timeout_decorator import timeout
import time


class Backtest():
    def __init__(self, strategy: Strategy.Strategy):
        self.strategy = strategy
        self.current_date = 0

    @timeout(10*60)
    def run_backtest(self, klines):
        if len(klines) > 0:
            for kline in klines:
                self.strategy.on_kline_day(kline)
                if self.current_date != kline.date:
                    self.strategy.on_daily_close()
                    self.current_date = kline.date
        self.strategy.on_complete()
        # print(self.strategy.context.account)
        return True

    @timeout(10*60)
    def run_backtest_by_snapshot(self, snapshots):
        if len(snapshots) > 0:
            for snapshot in snapshots:
                self.strategy.on_snapshot(snapshot)
                if self.current_date != snapshot.date:
                    self.strategy.on_daily_close()
                    self.current_date = snapshot.date
        self.strategy.on_complete()
        return True
